package reci.journal.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class DateTimeMacro extends Macro
{	
	private static String[] keys=new String[]{
		//date
		"%yyyy%","%yy%",
		"%MMMM%","%MMM%","%MM%","%M%",
		"%dddd%","%ddd%","%dd%","%d%",
		//time
		"%HH%","%H%","%hh%","%h%",
		"%A%","%A.%","%a%","%a.%",
		"%mm%","%m%",
		"%ss%","%s%"};
	
	private static Calendar calendar=null;
	
	/**
	 * Initializes to the current date and time.
	 */
	public static void init()
	{
		calendar=Calendar.getInstance();
		
		String[] values=new String[]{
			//date
			replace("YYYY"),replace("YY"),
			replace("MMMM"),replace("MMM"),replace("MM"),replace("M"),
			replace("EEEE"),replace("EEE"),replace("dd"),replace("d"),
			//time
			replace("HH"),replace("H"),replace("hh"),replace("h"),
			replace("a").toUpperCase(),Util.insertDots(replace("a").toUpperCase()),
				replace("a").toLowerCase(),Util.insertDots(replace("a").toLowerCase()),
			replace("mm"),replace("m"),
			replace("ss"),replace("s")
		};
		
		for(int i=0; i<keys.length; i++)
		{
			rules.put(keys[i],values[i]);
		}
	}
	
	/**
	 * @param pattern
	 * @return replacement
	 */
	private static String replace(String pattern)
	{
		return new SimpleDateFormat(pattern).format(calendar.getTime());
	}
}
